$NG.AllReady(function (page, { useValuesChange, useDataIndexChange, useUpdateRows, useUpdateRow }) {
    const mstform = $NG.getCmpApi("p_form_zsxmydsryc");
    const dgrid1 = $NG.getCmpApi("p_form_zsxmydsryc_d");
    const monthKey = ["", "u_yy", "u_ey", "u_sy", "u_siyue", "u_wy", "u_ly", "u_qy", "u_by", "u_jiuyue", "u_sey", "u_syy", "u_sey"];
  
    ////////////////////////////////////////////////////////////--mstform--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--mstform--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--mstform--////////////////////////////////////////////////////////////
    if (mstform) {
      cntMFilter();
      if (page.oType == "add") {
        mstform.getItem("u_nd").setValue({
          value: new Date().getFullYear(),
          label: new Date().getFullYear(),
        });
        mstform.getItem("u_yf").setValue({
          value: new Date().getMonth() + 1,
          label: new Date().getMonth() + 1 + "月",
        });
        getMonthPayByCenter();
        getYearPayPlanByPhidPc();
        mstform.getItem("bill_name").setValue(new Date().getFullYear() + "年" + new Date().getMonth() + 1 + "月度收入预测");
      }
      useValuesChange(({ args }) => {
        cntMFilter();
        getPtInfoByPhid();
        getMonthPayByCenter();
        getYearPayPlanByPhidPc();
        getCntCount("p_form_zsxmydsryc", "u_ht_name", () => {
          getCntMInfoByPhid();
        });
      }, "p_form_zsxmydsryc.phid_pc");
      useValuesChange(({ args }) => {
        getCntMInfoByPhid();
      }, "p_form_zsxmydsryc.u_ht_name");
      useValuesChange(({ args }) => {
        getMonthPayByCenter();
      }, "p_form_zsxmydsryc.u_qyzx");
      useValuesChange(({ args }) => {
        getRepaymentPlanByYM();
      }, "p_form_zsxmydsryc.u_month");
      useValuesChange(({ args }) => {
        getYearPayPlanByPhidPc();
        getRepaymentPlanByYM();
      }, "p_form_zsxmydsryc.uyear");
  
      getCntCount("p_form_zsxmydsryc", "u_ht_name", () => {
        getCntMInfoByPhid();
      });
    } else {
      console.log("mstform不存在");
    }
  
    ////////////////////////////////////////////////////////////--dgrid1--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--dgrid1--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--dgrid1--////////////////////////////////////////////////////////////
    useUpdateRow(({ args }) => {
      const map_ = args[0];
      console.log(args, map_.u_yuefen_EXName.substring(0, 1));
      map_.u_jh = window._lsz_catch_monthPay[monthKey[Number(map_.u_yuefen_EXName.substring(0, 1)) || 0]];
      if (window._lsz_catch_upYear) {
        map_.u_tq = window._lsz_catch_upYear[monthKey[Number(map_.u_yuefen_EXName.substring(0, 1)) || 0]];
        map_.u_tb = (Number(map_.u_tq) || 0) / (Number(map_.u_jh) || 0);
      }
      map_.u_mdyc = (Number(map_.u_bqyqrsr) || 0) + (Number(map_.u_bqsyyc) || 0);
      map_.u_jyc = (Number(map_.u_mdyc) || 0) - (Number(map_.u_bqsyyc) || 0);
      map_.u_jhwcl = (Number(map_.u_mdyc) || 0) / (Number(map_.u_jh) || 0);
      map_.u_rwwcl = (Number(map_.u_mdyc) || 0) / (Number(map_.u_ycxdrw) || 0);
      dgrid1.refreshView();
    }, "p_form_zsxmydsryc_d");
    ////////////////////////////////////////////////////////////--function--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--function--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--function--////////////////////////////////////////////////////////////
    //获取项目信息
    function getPtInfoByPhid() {
      if (mstform.getItem("phid_pc").getValue()) {
        $NG.execServer("getPtInfoByPhid", { phid: mstform.getItem("phid_pc").getValue() }, function (res) {
          if (res.count == 0) return;
          const datas = JSON.parse(res.data)[0].extendObjects;
          console.log(datas);
          $NG.updateState((updater) => {
            updater.data.p_form_zsxmydsryc.setProps({
              u_pc_no: datas.pc_no,
              //事业部
              u_syb: {
                value: datas.division_phid,
                label: datas.division_name,
              },
              //区域中心
              u_qyzx: {
                value: datas.regional_center_phid,
                label: datas.regional_center_name,
              },
              //产品线
              u_cpx: {
                value: datas.midwife_phid,
                label: datas.midwife_name,
              },
              //项目经理
              phid_emp: { value: datas.project_manager_phid, label: datas.project_manager_name },
            });
          });
        });
      }
    }
    function getYearPayPlanByPhidPc() {
      if (!mstform.getItem("phid_pc").getValue() || !mstform.getItem("u_nd").getValue()) return;
      $NG.execServer("getYearPayPlanByPhidPc", { phid_pc: mstform.getItem("phid_pc").getValue(), nd: mstform.getItem("u_nd").getValue() }, (res) => {
        if (res.count == 0) return;
        const _data = JSON.parse(res.data)[0].extendObjects;
        $NG.updateState((updater) => {
          updater.data.p_form_zsxmydsryc.setProps({
            u_ndzb_jh: _data.u_bnhj,
            u_ndzb_tq: _data.u_jynjh,
            u_ndzb_tb: ((Number(_data.u_jynjh) || 0) / (Number(_data.u_bnhj) || 0)).toFixed(2),
          });
        });
      });
      $NG.execServer("getYearPayPlanByPhidPc", { phid_pc: mstform.getItem("phid_pc").getValue(), nd: (Number(mstform.getItem("u_nd").getValue()) || 0) - 1 }, (res) => {
        if (res.count == 0) return;
        const _data = JSON.parse(res.data)[0].extendObjects;
        window._lsz_catch_upYear = { ..._data };
        console.log("缓存2", window._lsz_catch_upYear);
      });
    }
    //获取合同相关信息
    function getCntMInfoByPhid(params) {
      $NG.execServer("getCntMInfoByPhid", { phid: mstform.getItem("u_ht_name").getValue() }, function (res) {
        if (res.count == 0) return;
        const datas = JSON.parse(res.data)[0].extendObjects;
        console.log(datas);
        $NG.updateState((updater) => {
          updater.data.p_form_zsxmydsryc.setProps({
            u_htqdrq: datas.signdt, //合同签订日期
            u_sl: datas.user_taxrate, //税率
            u_ht_no: datas.pcm_no, //合同
            start_dt: datas.start_dt, //约定开工时间
            end_dt: datas.end_dt, //约定完工时间
            cnt_sum_vat_fc: datas.cnt_sum_vat_fc, //合同价税合计
            //产品线
            user_product_line: { value: datas.midwife_phid, label: datas.midwife_name },
            //合同类型
            pct_id: { value: datas.pct_id, label: datas.pct_name },
            //甲方单位
            customer: { value: datas.customer_id, label: datas.customer_name },
            //乙方单位
            supply: { value: datas.supply_id, label: datas.supply_name },
            //发票类型
            invoice_type_phid: { value: datas.invoice_type_phid, label: datas.invoice_type_name },
            //购方信息（客户信息）（收票单位信息）
            //单位名称
            tick_id: { value: datas.tick_id, label: datas.tick_name },
            //单位编码
            tick_no: datas.tick_no,
            //社会信用代码
            tick_unisocialcredit: datas.tick_unisocialcredit,
            //单位地址
            tick_taxaddress: datas.tick_taxaddress,
            //联系电话
            tick_taxtelephone: datas.tick_taxtelephone,
            //银行名称
            tick_taxbankid: { value: datas.tick_taxbankid, label: datas.tick_taxbankname },
            //银行账号
            tick_taxaccountno: datas.tick_taxaccountno,
            //销方信息（供应商信息）（开票单位信息）
            //单位名称
            inv_id: { value: datas.inv_id, label: datas.inv_name },
            //单位编码
            inv_no: datas.inv_no,
            //社会信用代码
            inv_unisocialcredit: datas.inv_unisocialcredit,
            //单位地址
            inv_taxaddress: datas.inv_taxaddress,
            //联系电话
            inv_taxtelephone: datas.inv_taxtelephone,
            //银行名称
            inv_taxbankid: { value: datas.inv_taxbankid, label: datas.inv_taxbankname },
            //银行账号
            inv_taxaccountno: datas.inv_taxaccountno,
          });
          getMonthPayByCenter();
        });
      });
    }
    function getMonthPayByCenter() {
      const phid_pc = String(mstform.getItem("phid_pc").getValue());
      const center = String(mstform.getItem("u_qyzx").getValue());
      console.log("=============================", phid_pc, center);
      if (!phid_pc) return;
      $NG.execServer("getMonthPayByCenter", { phid: phid_pc }, (res) => {
        console.log(res);
        if (res.count == 0) return;
        const _data = JSON.parse(res.data)[0].extendObjects;
        window._lsz_catch_monthPay = { ..._data };
        console.log(`缓存`, window._lsz_catch_monthPay);
        //默认增行
        const currentMonth = Number(mstform.getItem("u_yf").getValue()) || 0;
        dgrid1.clearRows().then(() => {
          console.log("增行");
          dgrid1.addRows({
            u_yuefen: currentMonth,
            u_yuefen_EXName: (Number(currentMonth) || 0) + "月",
            u_jh: window._lsz_catch_monthPay[monthKey[3]],
          });
        });
      });
    }
  
    /**
     * 过滤区
     * */
    //合同根据项目ID过滤
    cntMFilter();
    function cntMFilter() {
      $NG.updateUI((updater, state) => {
        console.log("过滤参数检查", updater);
        updater.fieldSetForm.p_form_zsxmydsryc.PCFieldSetPanelGroup1.u_ht_name.setProps({
          onBeforeOpen: async () => {
            var phid_pc = $NG.getCmpApi("p_form_zsxmydsryc").getItem("phid_pc").getValue();
            if (!phid_pc) {
              $NG.alert("请先选择项目");
              return false;
            } else {
              return true;
            }
          },
          ...(!!$NG.getCmpApi("p_form_zsxmydsryc").getItem("phid_pc").getValue() && {
            clientSqlFilter: {
              "pcm3_cnt_m.bill_type": 1, //合同类型
              "pcm3_cnt_m.phid_pc": $NG.getCmpApi("p_form_zsxmydsryc").getItem("phid_pc").getValue(),
            },
          }),
        });
      });
    }
    //获取合同数量
    function getCntCount(table, key, fun) {
      if (!$NG.getCmpApi(table).getItem("phid_pc").getValue()) return;
      $NG.execServer("getCntCount", { type: 1, phid_pc: $NG.getCmpApi(table).getItem("phid_pc").getValue() }, (res) => {
        console.log(res, "count");
        switch (res.count) {
          case 0:
            return;
          case 1:
            const datas = JSON.parse(res.data)[0].extendObjects;
            const temp_map = {};
            temp_map[key] = datas.phid;
            temp_map[`${key}_EXName`] = datas.bill_name;
            $NG.updateState((updater) => {
              updater.data[table].setProps({
                ...temp_map,
              });
              setTimeout(() => {
                if (fun) {
                  fun();
                }
              }, 200);
            });
        }
      });
    }
  });
  